I am tring to print just the the lists and tasks i get an [object object]
here is my code :
const users = {
user1: {
lists: [
{
id: 0,
name: "Work",
tasks: [
{
id: 0,
task: "task to do",
},
{
id: 1,
task: "task to do",
},
],
},
{
id: 1,
name: "Holiday",
tasks: [
{
id: 0,
task: "task to do",
},
{
id: 1,
task: "task to do",
},
],
},
{
id: 3,
name: "New List",
tasks: [],
},
],
},
};
and this what i tried:
$.each(users, function (key, value) {
const keys = key;
console.log(keys)
let test = users[key];
console.log(test)
$.each(test, function (key, value) {
value;
console.log(value)
$.each(value, function (key, value) {
value.name
document.write(value.name)
document.write(value.tasks)
})
});
});
this what it returns:
Work[object Object],[object Object]Holiday[object Object],[object Object]New List
my questions:
i am just beginner
First, I will ask that you try not to use jQuery ($) for everything when it's not needed.
Secondly, you have a variable called users, which I would think should be an array, but you have it down as a keyed object. Might be worth looking into.
Anyway, I'll try to help you with the "list alone" and "tasks alone"-stuff!
const users={user1:{lists:[{id:0,name:"Work",tasks:[{id:0,task:"task to do"},{id:1,task:"task to do"}]},{id:1,name:"Holiday",tasks:[{id:0,task:"task to do"},{id:1,task:"task to do"}]},{id:3,name:"New List",tasks:[]}]}}
const allLists = []
// id is "user1", user is the user-object.
for(const [id, user] of Object.entries(users)) {
const lists = user.lists
for(const list of lists) {
allLists.push(list)
}
}
console.log(`ALL LISTS: ${allLists}`) // Lots of "[object Object]"
console.log(`ALL LISTS: ${JSON.stringify(allLists)}`) // This one works